﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Cat.Domain.Services.Catalog;
using Forest.Cat.Domain;
using Forest.Cat.Domain.Shared;
using Forest.Cat.Web.Extensions;
using Forest.Cat.Web.Models.Catalog;
using Forest.Cat.Domain.Entities;
using Forest.Cat.Domain.Configuration;
using Forest.Cat.Domain.Services.Compnaies;

namespace Forest.Cat.Web.Controllers {
    public class HeadHunterController : Controller {
        private readonly IJobPositionService _jobPositionService;
        private readonly IWorkContext _workContext;
        private readonly INewsService _newsService;
        private readonly ILovService _lovService;
        private readonly ICompanyService _companyService;

        public HeadHunterController(IWorkContext workContext,
            IJobPositionService jobPositionService,
            INewsService newsService,
            ILovService lovService,
            ICompanyService companyService) {
            this._workContext = workContext;
            this._jobPositionService = jobPositionService;
            this._newsService = newsService;
            this._lovService = lovService;
            this._companyService = companyService;

        }

        public ActionResult Hunter() {
            return View();
        }

        public ActionResult Training() {
            return View();
        }

        public PartialViewResult NewsBlock(NewsTab newsTab) {
            return PartialView(_newsService.GetNewByTab(newsTab, 10).ToList().Select(x => x.ToModel()));
        }

        public PartialViewResult JobBlock(JobSource jobSource) {
            var jobList = _jobPositionService.GetJobsBySource(jobSource);

            return PartialView(jobList.Select(x => PrepareJobModelForView(x)).ToList());
        }

        [NonAction]
        private JobPositionModel PrepareJobModelForView(JobPosition job) {
            var company = _companyService.GetCompanyById(job.CompanyId.Value);
            //如果公司未找到，则跳转404
            if (company == null) {
                throw new NoDataFoundException();
            }
            var jobModel = job.ToModel();
            jobModel.YearsOfExperienceName = _lovService.GetLovName(job.YearsOfExperience.Value);
            jobModel.EducationLevelName = _lovService.GetLovName(job.EducationLevel.Value);
            jobModel.JobTypeName = _lovService.GetLovName(job.JobType);
            jobModel.SalaryRangeName = _lovService.GetLovName(job.SalaryRange.Value);
            var companyModel = company.ToModel();
            jobModel.CompanyModel = companyModel;
            jobModel.CityName = _lovService.GetLovName(job.City);

            return jobModel;
        }

    }
}
